<?php

class ProfileController extends ControllerBase
{
	public $defaultAction = 'profile';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return CMap::mergeArray(parent::filters(),array(
			'accessControl', // perform access control for CRUD operations
		));
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'users'=>Util::getSuperMods(),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	/**
	 * Shows a particular model.
	 */
	public function actionProfile()
	{
		$model = $this->loadUser();
	    $this->render('profile',array(
	    	'model'=>$model,
			'profile'=>$model->profile,
	    ));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionUpdate()
	{
		$model		= $this->loadUser();
		$profile	= $model->profile;

		if(isset($_POST['User']))
		{
			$model->attributes		= $_POST['User'];
			$profile->attributes	= $_POST['Profile'];

			// Connvert string to lowercase
			$profile->fullname		= Util::formatFullName($profile->fullname, true);
			// Clear all character non numberic
			$inputPhone				= $profile->phone;
			$inputCellPhone			= $profile->cellphone;
			$profile->phone			= Util::formatPhone($inputPhone, false);
			$profile->cellphone		= Util::formatPhone($inputCellPhone, false);

			$oldModel 			= User::model()->notsafe()->findByPk($model->id);
			$model->password	= $oldModel->password;

			if($model->validate()&& $profile->validate()) {
				if (isset($_FILES['Profile'])) {
					$oldAvatar	= $oldModel->profile->avatar;
					if (isset($_FILES['Profile']['size']['avatar']) && $_FILES['Profile']['size']['avatar'] > 0) {
						$avatar 			= CUploadedFile::getInstance($profile, 'avatar');
						$tmpNm				= time()-1;
	                	$imageName 			= strtolower($tmpNm . "." . $avatar->getExtensionName());
	                	$profile->avatar	= $imageName;
	                	$avatar->saveAs(Util::pathUpload('avatar') .'/' . $imageName);


						if (($oldAvatar != '') && file_exists(Util::pathUpload('slide') .'/' . $oldAvatar)) {
	                		unlink(Util::pathUpload('avatar') .'/' . $oldAvatar);
	                	}
					}
					else {
						$profile->avatar 	= $oldAvatar;
					}
	            }
	            if ($model->save()) {
	            	if ($profile->save()) {
						$arrUrl = array('profile');
						if (isset($_GET['ajax'])) {
							$arrUrl['ajax'] = true;
						}
						$this->redirect($arrUrl);
	            	}
	            }

			}else $profile->validate();

			$profile->fullname 	= Util::formatFullName($profile->fullname);
			$profile->phone		= $inputPhone;
			$profile->cellphone	= $inputCellPhone;
		}

		$this->render('edit',compact('model', 'profile'));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionEdit()
	{
		$model = $this->loadUser();
		$profile=$model->profile;

		// ajax validator
		if(isset($_POST['ajax']) && $_POST['ajax']==='profile-form')
		{
			echo ActiveFormBase::validate(array($model,$profile));
			Yii::app()->end();
		}

		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			$profile->attributes=$_POST['Profile'];

			if($model->validate()&&$profile->validate()) {
				$model->save();
				$profile->save();
                Yii::app()->user->updateSession();
				Yii::app()->user->setFlash('profileMessage',UserModule::t("Changes is saved."));
				$this->redirect(array('/user/profile'));
			} else $profile->validate();
		}

		$this->render('edit',array(
			'model'=>$model,
			'profile'=>$profile,
		));
	}

	/**
	 * Change password
	 */
	public function actionChangepassword() {
		$model = new UserChangePassword;
		if (Yii::app()->user->id) {

			// ajax validator
			if(isset($_POST['ajax']) && $_POST['ajax']==='changepassword-form')
			{
				echo ActiveFormBase::validate($model);
				Yii::app()->end();
			}

			if(isset($_POST['UserChangePassword'])) {
					$model->attributes=$_POST['UserChangePassword'];
					if($model->validate()) {
						$new_password = User::model()->notsafe()->findbyPk(Yii::app()->user->id);
						$new_password->password = UserModule::encrypting($model->password);
						$new_password->activkey=UserModule::encrypting(microtime().$model->password);
						$new_password->save();
						Yii::app()->user->setFlash('profileMessage',UserModule::t("New password is saved."));
						$this->redirect(array("profile"));
					}
			}
			$this->render('changepassword',array('model'=>$model));
	    }
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
	 */
	public function loadUser()
	{
		if($this->_model===null)
		{
			if(Yii::app()->user->id)
				$this->_model=Yii::app()->controller->module->user();
			if($this->_model===null)
				$this->redirect(Yii::app()->controller->module->loginUrl);
		}
		return $this->_model;
	}
}